package io.kerns.boot.web.current;

import java.util.HashMap;
import java.util.Map;

/**
 * 当前用户信息请求上下文,比如用户ID，用户头像之类的一些信息
 *
 * @author xiaohei
 * @create 2020-10-15 上午9:57
 **/
public final class RequestContext {

    private static InheritableThreadLocal<Map<String, Object>> threadLocal = new InheritableThreadLocal();

    private RequestContext() {
    }

    private static void init() {
        threadLocal.set(new HashMap(20));
    }

    private static boolean isInit() {
        return threadLocal.get() != null;
    }

    private static <T> T getData(String key) {
        return threadLocal.get() == null ? null : (T) ((Map) threadLocal.get()).get(key);
    }

    private static void putInLocal(String key, Object value) {
        if (!isInit()) {
            init();
        }
        ((Map) threadLocal.get()).put(key, value);
    }

   static void clear(){
        threadLocal.remove();;
    }
}
